﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

using cocos2d;

namespace RunCircle
{
    public class MenuNode:CCSprite
    {
        protected SEL_MenuHandler callback;
        protected CCSpriteFrame normalSprite;
        protected CCSpriteFrame selectSprite;
        protected CCRect rect;
        protected bool HOver;
        public bool Enabled { set; get; }
        protected ButtonState oldState;
        public MenuNode(string normalName,
                        string selectName,
                        SEL_MenuHandler Func)
        {
            normalSprite = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(normalName);
            selectSprite = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(selectName);
            callback = Func;
            HOver = false;
            Enabled = true;
            //
            //
            if (initWithSpriteFrame(normalSprite))
            {
                rect = new CCRect(normalSprite.Rect.origin.x,
                                  normalSprite.Rect.origin.y,
                                  normalSprite.Rect.size.width,
                                  normalSprite.Rect.size.height);
            }
            //
            
            
        }
        //
        public override void update(float dt)
        {
            if (Enabled)
            {
                MouseState mouseState = Mouse.GetState();
                //
                CCPoint pos = new CCPoint(mouseState.X, mouseState.Y);
                CCRect wRect = new CCRect();
                pos.y = CCDirector.sharedDirector().getWinSize().height - pos.y;
                wRect.origin = new CCPoint(position.x, position.y);
                wRect.size = new CCSize(rect.size.width, rect.size.height);
                wRect.origin.x -= wRect.size.width / 2;
                wRect.origin.y -= wRect.size.height / 2;

                if (CCRect.CCRectContainsPoint(wRect, pos))
                {
                    HOver = true;
                    this.initWithSpriteFrame(selectSprite);
                }
                else
                {
                    HOver = false;
                    this.initWithSpriteFrame(normalSprite);
                }
                //
                if (mouseState.LeftButton == ButtonState.Released
                    && oldState == ButtonState.Pressed
                    && HOver == true)
                {
                    callback(this);                   
                }
                //
                oldState = mouseState.LeftButton;
            }           
            base.update(dt);
        }

        public override void draw()
        {
//             ccColor4F color = new ccColor4F(1.0f, 0, 0, 1.0f);
//             CCSize wh = new CCSize(rect.size.width,
//                                    rect.size.height);
//             CCPoint op = new CCPoint(rect.origin.x,
//                                        rect.origin.y);
//             CCPoint op_0 = position;
//             CCPoint op_1 = new CCPoint(op_0.x + wh.width, op_0.y);
//             CCPoint op_3 = new CCPoint(op_0.x, op_0.y + wh.height);
//             CCPoint op_2 = new CCPoint(op_0.x + wh.width, op_0.y + wh.height);
// 
//             CCPoint[] points = new CCPoint[] { op_0, op_1, op_2, op_3 };
// 
//             CCDrawingPrimitives.ccDrawPoly(points, 4, true, color);
            //---------------
//             MouseState mouseState = Mouse.GetState();
//             CCPoint pos = new CCPoint(mouseState.X, mouseState.Y);
//             pos.y = CCDirector.sharedDirector().getWinSize().height - pos.y;
//             ccColor4B colorb = new ccColor4B(0, 200, 0, 200);
//             CCDrawingPrimitives.ccDrawCircle(pos, 20.0f, 0.0f, 6, true, colorb);
            //

            base.draw();
        }
    }
}
